#include <dre_tun.h>

int tun_open(char *dev, int istun)
{
   struct ifreq ifr;
    int fd;

    if((fd = open("/dev/net/tun",O_RDWR)) < 0) return -1;

    memset(&ifr, 0, sizeof(ifr));

    ifr.ifr_flags = (istun ? IFF_TUN : IFF_TAP) | IFF_NO_PI;
    if(*dev)
        strncpy(ifr.ifr_name, dev, IFNAMSIZ);

    if(ioctl(fd, TUNSETIFF, (void*)&ifr) < 0) return -1;

    strcpy(dev, ifr.ifr_name);

    return fd;
}

int tun_close(int fd)
{
    return close(fd);
}
